<?php
namespace App\Http\Controllers\Api;

use App\Services\UserOrderService;
use Carbon\Carbon;
use Framework\BaseController;
use Framework\Hope;
use GTAuthRequest;
use GTConfig;
use GTPushApi;
use GTStatisticsApi;
use GTUserApi;
use http\Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Kra8\Snowflake\Snowflake;

class TestController extends BaseController
{


    //获取毫秒时间戳
    public  function getMicroTime()
    {
        list($usec, $sec) = explode(" ", microtime());
        $time = ($sec . substr($usec, 2, 3));
        return $time;
    }


    public function test()
    {
        dd(Hope::getRedis());
        $snowflake = app('Kra8\Snowflake\Snowflake');
        $request_id = $snowflake->next();
        $client_id = '4933ede76ddd0f44f77bed22884ba329';
        $title = '第一次推送';
        $content = '点击我进入应用首页';
        $send_type = 'startapp';
        $res = $this->pushToSingleByCid($request_id,$client_id,$title,$content,$send_type);
        $this->retSuccess($res);
    }

    public function pushToSingleByCid($send_id,$client_id, $send_title,$send_content,$send_type='none')
    {
        $app_key =   'uLECz9fepLArob2HJkU3K';
        $app_id =   'm8ARnFoNH269Urgqy4oRL8';
        $master_secret =   'wgumdLLM8M9GmGVanTmzN';

        //创建API，APPID等配置参考 环境要求 进行获取
        $api = new \GTClient("https://restapi.getui.com",
            $app_key,
            $app_id,
            $master_secret
        );
        $auth = $api ->getAuthToken();

        //设置推送参数
        $push = new \GTPushRequest();
        //请求唯一标识号，自己生成，类似于订单号 10-32位之间；如果request_id重复，会导致消息丢失
        $push->setRequestId($send_id);
        $message = new \GTPushMessage();
        $notify = new \GTNotification();
        //推送参数参考 https://docs.getui.com/getui/server/rest_v2/common_args/?id=doc-title-6
        //通知消息标题，长度 ≤ 50
        $notify->setTitle($send_title);
        //通知消息内容，长度 ≤ 256
        $notify->setBody($send_content);
        //点击通知后续动作，目前支持以下后续动作:
        //1、intent：打开应用内特定页面url：打开网页地址。
        //2、payload：自定义消息内容启动应用。
        //3、payload_custom：自定义消息内容不启动应用。
        //4、startapp：打开应用首页。
        //5、none：纯通知，无后续动作
        $notify->setClickType($send_type);
        $message->setNotification($notify);
        $push->setPushMessage($message);
        //设备CID很重要，需要在用户登录时，存储到用户表
        $push->setCid($client_id);
        //处理返回结果 pushBatchByCid
        $result = $api->pushApi()->pushToSingleByCid($push);
        return $result;
    }

    public function post_im($servicename,$command)
    {
        $SDKAppID = config('');
        $identifier = config('');
        $usersig = config('');
        $host = "https://console.tim.qq.com/v4";
        $url = $host . $servicename. $command;
        $client = new \GuzzleHttp\Client();
        $res = $client->request('POST',$url,[
            'form_params' => [
                'sdkappid' => $SDKAppID,
                'identifier' => $identifier,
                'usersig' => $usersig,
                'random' => Str::random(9),
                'contenttype' => 'json',
            ]
        ]);
        if($res->getStatusCode() != 200){
            $this->retError("请求失败！");
        }else{
            $res = json_decode($res->getBody(),true);
            $this->retSuccess($res);
        }
    }


    // todo 测试订单已支付
    public function test_order_pay_ok(Request $request , UserOrderService $userOrderService)
    {

        $order_id = $request->input('order_id', 0);
        $pay_sn = $request->input('pay_sn', 0);
        $pay_type = $request->input('pay_type', 0);
        if(!$order_id){
            $this->retError('缺少订单');
        }
        $res = $userOrderService->orderPaySuccessAfter($order_id, $pay_sn, $pay_type);
        $this->retSuccess($res);

    }
}
